<template>
  <view class="container">
    <view class="header">
      <text class="title">OOM ADJ 插件示例</text>
    </view>
    
    <view class="content">
      <!-- 当前状态显示 -->
      <view class="status-card">
        <text class="card-title">当前状态</text>
        <view class="status-item">
          <text class="label">进程ID:</text>
          <text class="value">{{ currentStatus.pid }}</text>
        </view>
        <view class="status-item">
          <text class="label">OOM ADJ:</text>
          <text class="value">{{ currentStatus.oomAdj }}</text>
        </view>
        <view class="status-item">
          <text class="label">状态:</text>
          <text class="value" :class="currentStatus.success ? 'success' : 'error'">
            {{ currentStatus.message }}
          </text>
        </view>
      </view>
      
      <!-- 操作按钮 -->
      <view class="button-group">
        <button class="btn btn-primary" @click="getCurrentOomAdj">
          获取当前 OOM ADJ
        </button>
        
        <view class="input-group">
          <text class="input-label">设置 OOM ADJ 值:</text>
          <input 
            class="input" 
            type="number" 
            v-model="inputOomAdj" 
            placeholder="输入 -17 到 15 之间的值" />
          <button class="btn btn-secondary" @click="setCurrentOomAdj">
            设置 OOM ADJ
          </button>
        </view>
      </view>
      
      <!-- 预设值快捷按钮 -->
      <view class="preset-group">
        <text class="group-title">快捷设置</text>
        <view class="preset-buttons">
          <button class="preset-btn" @click="setPresetOomAdj(-16)">
            持久化进程 (-16)
          </button>
          <button class="preset-btn" @click="setPresetOomAdj(-8)">
            前台应用 (-8)
          </button>
          <button class="preset-btn" @click="setPresetOomAdj(0)">
            可见进程 (0)
          </button>
          <button class="preset-btn" @click="setPresetOomAdj(9)">
            后台进程 (9)
          </button>
        </view>
      </view>
      
      <!-- 操作日志 -->
      <view class="log-card">
        <text class="card-title">操作日志</text>
        <scroll-view class="log-content" scroll-y="true">
          <view v-for="(log, index) in logs" :key="index" class="log-item">
            <text class="log-time">{{ log.time }}</text>
            <text class="log-message">{{ log.message }}</text>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentStatus: {
        pid: 0,
        oomAdj: 0,
        success: false,
        message: '未获取'
      },
      inputOomAdj: 0,
      logs: []
    }
  },
  
  onLoad() {
    this.getCurrentOomAdj()
  },
  
  methods: {
    // 获取当前 OOM ADJ 值
    getCurrentOomAdj() {
      try {
        const oomAdjModule = uni.requireNativePlugin('uni-oom-adj')
        const result = oomAdjModule.getOomAdj()
        
        this.currentStatus = {
          pid: result.pid,
          oomAdj: result.oomAdj,
          success: result.success,
          message: result.message
        }
        
        this.addLog(`获取 OOM ADJ: ${result.success ? '成功' : '失败'} - ${result.message}`)
        
        if (result.success) {
          uni.showToast({
            title: `当前 OOM ADJ: ${result.oomAdj}`,
            icon: 'success'
          })
        } else {
          uni.showToast({
            title: '获取失败',
            icon: 'error'
          })
        }
      } catch (error) {
        this.addLog(`获取 OOM ADJ 异常: ${error.message}`)
        uni.showToast({
          title: '插件调用失败',
          icon: 'error'
        })
      }
    },
    
    // 设置 OOM ADJ 值
    setCurrentOomAdj() {
      const oomAdj = parseInt(this.inputOomAdj)
      
      if (isNaN(oomAdj) || oomAdj < -17 || oomAdj > 15) {
        uni.showToast({
          title: 'OOM ADJ 值必须在 -17 到 15 之间',
          icon: 'none'
        })
        return
      }
      
      this.setOomAdjValue(oomAdj)
    },
    
    // 设置预设的 OOM ADJ 值
    setPresetOomAdj(oomAdj) {
      this.inputOomAdj = oomAdj
      this.setOomAdjValue(oomAdj)
    },
    
    // 执行设置 OOM ADJ
    setOomAdjValue(oomAdj) {
      try {
        const oomAdjModule = uni.requireNativePlugin('uni-oom-adj')
        const result = oomAdjModule.setOomAdj(oomAdj)
        
        this.addLog(`设置 OOM ADJ 为 ${oomAdj}: ${result.success ? '成功' : '失败'} - ${result.message}`)
        
        if (result.success) {
          uni.showToast({
            title: `设置成功，当前值: ${result.oomAdj}`,
            icon: 'success'
          })
          // 刷新当前状态
          this.getCurrentOomAdj()
        } else {
          uni.showToast({
            title: `设置失败: ${result.message}`,
            icon: 'none',
            duration: 3000
          })
        }
      } catch (error) {
        this.addLog(`设置 OOM ADJ 异常: ${error.message}`)
        uni.showToast({
          title: '插件调用失败',
          icon: 'error'
        })
      }
    },
    
    // 添加日志
    addLog(message) {
      const now = new Date()
      const time = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`
      
      this.logs.unshift({
        time: time,
        message: message
      })
      
      // 限制日志数量
      if (this.logs.length > 50) {
        this.logs = this.logs.slice(0, 50)
      }
    }
  }
}
</script>

<style scoped>
.container {
  flex: 1;
  background-color: #f5f5f5;
}

.header {
  height: 100px;
  background-color: #007aff;
  justify-content: center;
  align-items: center;
}

.title {
  font-size: 20px;
  color: #ffffff;
  font-weight: bold;
}

.content {
  padding: 20px;
}

.status-card, .log-card {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.card-title {
  font-size: 18px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 15px;
}

.status-item {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.label {
  font-size: 16px;
  color: #666666;
}

.value {
  font-size: 16px;
  color: #333333;
  font-weight: bold;
}

.success {
  color: #52c41a;
}

.error {
  color: #ff4d4f;
}

.button-group {
  margin-bottom: 20px;
}

.btn {
  height: 45px;
  border-radius: 8px;
  margin-bottom: 15px;
  font-size: 16px;
  font-weight: bold;
}

.btn-primary {
  background-color: #007aff;
  color: #ffffff;
}

.btn-secondary {
  background-color: #52c41a;
  color: #ffffff;
}

.input-group {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 15px;
}

.input-label {
  font-size: 16px;
  color: #333333;
  margin-bottom: 10px;
}

.input {
  height: 40px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  padding: 0 12px;
  font-size: 16px;
  margin-bottom: 15px;
}

.preset-group {
  background-color: #ffffff;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 20px;
}

.group-title {
  font-size: 16px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 15px;
}

.preset-buttons {
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
}

.preset-btn {
  width: 48%;
  height: 40px;
  background-color: #f0f0f0;
  border-radius: 6px;
  margin-bottom: 10px;
  font-size: 14px;
  color: #333333;
}

.log-content {
  max-height: 200px;
}

.log-item {
  flex-direction: row;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.log-time {
  width: 80px;
  font-size: 12px;
  color: #999999;
}

.log-message {
  flex: 1;
  font-size: 14px;
  color: #333333;
}
</style>